package com.example.medicalequipment.ui.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.fragment.app.Fragment;

import com.example.medicalequipment.R;

public class NewsFragment extends Fragment {

    private WebView webView;
    private ProgressDialog progressDialog; // 加载进度对话框

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, container, false);

        webView = view.findViewById(R.id.webView);
        initWebView();
        loadUrl("https://www.nmpa.gov.cn/ylqx/ylqxggtg/index.html");

        return view;
    }

    private void initWebView() {
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setUserAgentString("Mozilla/5.0 (Android)");

        // 设置 WebChromeClient 显示加载进度
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    hideProgressDialog(); // 加载完成后隐藏进度条
                } else {
                    showProgressDialog(); // 显示加载进度条
                }
                super.onProgressChanged(view, newProgress);
            }
        });

        // 设置 WebViewClient 防止黑屏（优先显示空白页）
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) {
                webView.setBackgroundColor(getResources().getColor(android.R.color.white)); // 设置背景为白色
                super.onPageStarted(view, url, favicon);
            }
        });
    }

    private void loadUrl(String url) {
        webView.loadUrl(url);
    }

    // 显示加载进度对话框
    private void showProgressDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(requireContext());
            progressDialog.setMessage("加载中...");
            progressDialog.setCancelable(false);
        }
        progressDialog.show();
    }

    // 隐藏加载进度对话框
    private void hideProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    // 返回键监听：优先返回 WebView 上一页，否则退出当前 Fragment
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new androidx.activity.OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                if (webView.canGoBack()) { // 判断是否有上一页
                    webView.goBack(); // 返回 WebView 上一页
                } else {
                    requireActivity().getSupportFragmentManager().popBackStack();
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        webView.destroy();
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }
}